package com.business.api.official.toursism.websiteshop.controller;

import com.business.api.official.toursism.websiteshop.dto.WebsiteShopApiDTO;
import com.business.api.official.toursism.websiteshop.from.WebsiteShopApiSaveFrom;
import com.business.api.official.toursism.websiteshop.from.WebsiteShopApiUpdateFrom;
import com.business.api.official.toursism.websiteshop.manager.WebsiteShopApiManager;
import com.business.api.official.toursism.websiteshop.query.WebsiteShopApiQuery;
import com.business.api.official.toursism.websiteshop.query.WebsiteShopApiPageQuery;
import com.business.api.official.toursism.websiteshop.service.WebsiteShopApiService;
import com.business.api.official.toursism.websiteshop.vo.WebsiteShopApiBriefPageVO;
import com.business.api.official.toursism.websiteshop.vo.WebsiteShopApiDetailPageVO;
import com.business.api.official.toursism.websiteshop.vo.WebsiteShopApiDetailVO;
import com.business.api.official.toursism.websiteshop.vo.WebsiteShopApiDetailListVO;
import com.business.api.official.toursism.websiteshop.vo.data.WebsiteShopApiDetailData;
import com.business.system.official.toursism.websiteshop.entity.WebsiteShop;
import com.seed.core.exception.ManagerException;
import com.seed.core.exception.ServiceException;
import com.seed.core.pojo.controller.SeedApiController;
import com.seed.core.pojo.dto.SeedMessageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @description 购物车
 * @author CodeGenerator
 * @date   2025-01-25
 * @version V1.0
 */
@Slf4j
@RestController(value = "websiteShopApiController")
@Api(value = "websiteShopController", tags = "购物车")
@RequestMapping("/api/v1/toursism/websiteShop")
public class WebsiteShopApiController extends SeedApiController<WebsiteShopApiService, WebsiteShopApiManager, WebsiteShop> {

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "保存")
    public WebsiteShopApiDetailVO save(@RequestBody @ApiParam(value = "保存表单", required = true) WebsiteShopApiSaveFrom websiteShopFrom) {
        log.info("[api-v1]WebsiteShopController.save,保存");

        WebsiteShopApiDetailVO result = new WebsiteShopApiDetailVO();
        if (StringUtils.isEmpty(websiteShopFrom)) {
            result.setCode("A0410");
            result.setMessage("表单不能为空!");
            result.setSuccess(false);
            return result;
        }
        WebsiteShopApiDTO apiDTO;
        try {
            apiDTO = baseService.save(websiteShopFrom);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteShopController.save ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("保存失败");
            return result;
        }
        if (!apiDTO.getSuccess()) {
            result.setCode(apiDTO.getCode());
            result.setSuccess(false);
            result.setMessage(apiDTO.getMessage());
            return result;
        }
        WebsiteShopApiDetailData data;
        try {
            data = baseManager.convertApiDetailData(apiDTO.getWebsiteShop());
        } catch (ManagerException e) {
            log.error("[api-v1]WebsiteShopController.save ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("保存失败");
            return result;
        }
        result.setData(data);
        return result;
    }

    @RequestMapping(method = RequestMethod.PATCH)
    @ResponseBody
    @ApiOperation(value = "不完全更新,更新from中不为空的值（空值不会被更新）")
    public WebsiteShopApiDetailVO updateSelective(@RequestBody @ApiParam(value = "更新表单", required = true) WebsiteShopApiUpdateFrom websiteShopFrom) {
        log.info("[api-v1]WebsiteShopController.updateSelective,不完全更新,更新from中不为空的值（空值不会被更新）");

        WebsiteShopApiDetailVO result = new WebsiteShopApiDetailVO();
        if (StringUtils.isEmpty(websiteShopFrom)) {
            result.setCode("A0410");
            result.setMessage("更新信息不能为空!");
            result.setSuccess(false);
            return result;
        }
        if (StringUtils.isEmpty(websiteShopFrom.getId())) {
            result.setCode("A0410");
            result.setMessage("id信息不能为空!");
            result.setSuccess(false);
            return result;
        }
        WebsiteShopApiDTO apiDTO;
        try {
            apiDTO = baseService.updateSelectiveById(websiteShopFrom);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteShopController.updateSelective ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("更新失败");
            return result;
        }
        if (!apiDTO.getSuccess()) {
            result.setCode(apiDTO.getCode());
            result.setSuccess(false);
            result.setMessage(apiDTO.getMessage());
            return result;
        }
        WebsiteShopApiDetailData data;
        try {
            data = baseManager.convertApiDetailData(websiteShopFrom.getId());
        } catch (ManagerException e) {
            log.error("[api-v1]WebsiteShopController.updateSelective ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("更新失败");
            return result;
        }
        result.setData(data);
        return result;
    }

    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "删除,支持ids")
    public WebsiteShopApiDetailVO delete(@PathVariable @ApiParam(value = "主键id", required = true) String ids) {
        log.info("[api-v1]WebsiteShopController.delete,删除");

        WebsiteShopApiDetailVO result = new WebsiteShopApiDetailVO();
        if (StringUtils.isEmpty(ids)) {
            result.setCode("A0410");
            result.setMessage("id不能为空!");
            result.setSuccess(false);
            return result;
        }
        SeedMessageDTO apiDTO;
        try {
            List<String> idList = Arrays.asList(ids.split(","));
            apiDTO = baseService.removeByIds(idList);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteShopController.delete ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("删除失败");
            return result;
        }
        if (!apiDTO.getSuccess()) {
            result.setCode(apiDTO.getCode());
            result.setSuccess(false);
            result.setMessage(apiDTO.getMessage());
            return result;
        }
        return result;
    }


    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id获取购物车详细信息")
    public WebsiteShopApiDetailVO getDetailVO(@PathVariable @ApiParam(value = "主键id", required = true) String id) {
        log.info("[api-v1]WebsiteShopController.getWebsiteShopApiVO接口,根据id获取购物车详细信息");

        WebsiteShopApiDetailVO result = new WebsiteShopApiDetailVO();
        if (StringUtils.isEmpty(id)) {
            result.setCode("A0410");
            result.setMessage("id不能为空!");
            result.setSuccess(false);
            return result;
        }

        WebsiteShopApiDetailData data;
        try {
            data = baseManager.convertApiDetailData(id);
        } catch (ManagerException e) {
            log.error("[api-v1]WebsiteShopController.getWebsiteShopApiVO接口 ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }
        result.setData(data);
        return result;
    }


    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取全部")
    public WebsiteShopApiDetailListVO listAll(@ApiParam(value = "查询条件", required = true) WebsiteShopApiQuery query) {
        log.info("[api-v1]WebsiteShopController.listAll,获取全部");
        WebsiteShopApiDetailListVO result = new WebsiteShopApiDetailListVO();
        if (StringUtils.isEmpty(query)) {
            query = new WebsiteShopApiQuery();
        }
        try {
            result = baseService.listAll(query);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteShopController.listAll ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }

        return result;
    }

    @RequestMapping(value = "/brief", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据查询条件查询简略对象")
    public WebsiteShopApiBriefPageVO getBriefPageVO(@ApiParam(value = "查询条件", required = true) WebsiteShopApiPageQuery query) {
        log.info("[api-v1]WebsiteShopController.getBriefPageVO,根据查询条件查询简略对象");

        WebsiteShopApiBriefPageVO result = new WebsiteShopApiBriefPageVO();
        if (StringUtils.isEmpty(query)) {
            query = new WebsiteShopApiPageQuery();
        }

        try {
            result = baseService.getBriefPageVO(query);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteShopController.getBriefPageVO ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }

        return result;
    }


    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据查询条件查询详细对象")
    public WebsiteShopApiDetailPageVO getDetailPageVO(@ApiParam(value = "查询条件", required = true) WebsiteShopApiPageQuery query) {
        log.info("[api-v1]WebsiteShopController.getDetailPageVO,根据查询条件查询详细对象");

        WebsiteShopApiDetailPageVO result = new WebsiteShopApiDetailPageVO();
        if (StringUtils.isEmpty(query)) {
            query = new WebsiteShopApiPageQuery();
        }

        try {
            result = baseService.getDetailPageVO(query);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteShopController.getDetailPageVO ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }

        return result;
    }

}